#include "PhiysicsSprite.h"

void removeShape( cpBody *body, cpShape *shape, void *data )
{
	cpShapeFree( shape );
}

CPhiysicsSprite::CPhiysicsSprite(void)
: m_pBody(NULL)
{
}

CPhiysicsSprite::~CPhiysicsSprite(void)
{
	cpBodyEachShape(m_pBody, removeShape, NULL);
	cpBodyFree( m_pBody );
}

void CPhiysicsSprite::setPhysicsBody(cpBody * body)
{
	m_pBody = body;
}

bool CPhiysicsSprite::isDirty(void)
{
	return true;
}

CCAffineTransform CPhiysicsSprite::nodeToParentTransform(void)
{
	float x = m_pBody->p.x;
	float y = m_pBody->p.y;

	if ( isIgnoreAnchorPointForPosition() ) {
		x += m_tAnchorPointInPoints.x;
		y += m_tAnchorPointInPoints.y;
	}

	// Make matrix
	float c = m_pBody->rot.x;
	float s = m_pBody->rot.y;

	if( ! m_tAnchorPointInPoints.equals(CCPointZero) ){
		x += c*-m_tAnchorPointInPoints.x + -s*-m_tAnchorPointInPoints.y;
		y += s*-m_tAnchorPointInPoints.x + c*-m_tAnchorPointInPoints.y;
	}

	// Rot, Translate Matrix
	m_tTransform = CCAffineTransformMake( c,  s,
		-s,    c,
		x,    y );

	return m_tTransform;
}